package com.leesche.zcsp.node.app.v1.controller.speed.device;

import com.leesche.zcsp.node.app.v1.controller.operate.AppPlaceController;
import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.dao.PubTemplateDevDao;
import com.leesche.zcsp.node.web.model.PubDevTemplate;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.model.PubTemplateDev;
import com.leesche.zcsp.node.web.service.PubDevTemplateService;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubTemplateDevService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by dennies yang  on 2017-02-08.
 */
@Controller
@RequestMapping(value = "/app/v1/testSpeed/device")
public class TemplateDevController {
    private final static Logger LOGGER = LoggerFactory.getLogger(TemplateDevController.class);

    @Autowired
    private PubTemplateDevService templateDevService;


    @Autowired
    PrivilegeService privilegeService;

    @Autowired
    private PubDeviceService deviceService;
    /**
     * 模板列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "listTemplateDevsByPage", method = RequestMethod.POST)
    public Map<String,Object> listView(
            Long templateid,
            Integer page,Integer  rows
    ){
        try {
//            Long adminId = 1L;
            PubTemplateDevCondition condition=new PubTemplateDevCondition();
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            condition.setAdminId(adminId);
            condition.setTemplateid(templateid);
            condition.setPage(page);
            condition.setRows(rows);
            PagingResult<PubTemplateDevDto> list = templateDevService.findAllByPage(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * 模板列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "listTemplateDevs", method = RequestMethod.POST)
    public Map<String,Object> listView(
            Long templateid
    ){
        try {

//            Long adminId = 1L;
            PubTemplateDevCondition condition=new PubTemplateDevCondition();
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            condition.setAdminId(adminId);
            condition.setTemplateid(templateid);
            List<PubTemplateDevDto> list = templateDevService.findListsByCondition(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 添加模板
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addTemplateDev", method = RequestMethod.POST)
    public Map<String,Object> add(
            PubTemplateDev dev
    ){
        try {
            PubDeviceDto deviceDto=null;
//            Long adminId = 1L;
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            if(dev.getTemplatetype()==1){
                QueryPubDeviceCondition condition=new QueryPubDeviceCondition();
                condition.setImme(dev.getDevvalue());
                 deviceDto=deviceService.findByCondition(condition);
                if(deviceDto==null){
                    return WebResult.failure("设备IMEI码/MAC不存在!").toMap();
                }
            } else if(dev.getTemplatetype()==2){
                QueryPubDeviceCondition condition=new QueryPubDeviceCondition();
                condition.setDeviceshortcode(dev.getDevvalue());
                deviceDto=deviceService.findByCondition(condition);
                if(deviceDto==null){
                    return WebResult.failure("设备简码不存在!").toMap();
                }
            }


            dev.setDeviceid(deviceDto.getDeviceid());
            dev.setAdminid(adminId);
            dev.setCreatetime(new Date());
            dev.setUpdatetime(new Date());
            templateDevService.insert(dev);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editTemplateDev", method = RequestMethod.POST)
    public Map<String,Object> edit(
            PubTemplateDev dev
    ){
        try {
            PubTemplateDev oldDev=templateDevService.selectByPrimaryKey(dev.getDevtemplateid());
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            if(oldDev.getAdminid()==null||!oldDev.getAdminid().equals(adminId)){
                return WebResult.failure("你无权限!").toMap();
            }
            if(dev.getTemplatetype()==1){
                QueryPubDeviceCondition condition=new QueryPubDeviceCondition();
                condition.setImme(dev.getDevvalue());
                PubDeviceDto deviceDto=deviceService.findByCondition(condition);
                if(deviceDto==null){
                    return WebResult.failure("设备IMEI码/MAC不存在!").toMap();
                }
            }
            else if(dev.getTemplatetype()==2){
                QueryPubDeviceCondition condition=new QueryPubDeviceCondition();
                condition.setDeviceshortcode(dev.getDevvalue());
                PubDeviceDto deviceDto=deviceService.findByCondition(condition);
                if(deviceDto==null){
                    return WebResult.failure("设备简码不存在!").toMap();
                }
            }

            dev.setAdminid(adminId);
            dev.setCreatetime(new Date());
            dev.setUpdatetime(new Date());
            templateDevService.updateByPrimaryKey(dev);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

}
